﻿namespace Authentication.Data.DataContextStorage
{
    using System.Data.Entity;
    using System.Web; 
    public class HttpDataContextStorageContainer : IDataContextStorageContainer
    {
        /// <summary>
        ///     The _data context key.
        /// </summary>
        private string _dataContextKey = "DataContext";

        /// <summary>
        ///     The get data context.
        /// </summary>
        /// <returns>
        ///     The <see cref="AuthenticationEntities" />.
        /// </returns>
        public DbContext GetDataContext()
        {
            AuthenticationEntities objectContext = null;
            if (HttpContext.Current.Items.Contains(_dataContextKey))
            {
                objectContext = (AuthenticationEntities)HttpContext.Current.Items[_dataContextKey];
            }

            return objectContext;
        }

        /// <summary>
        /// The store.
        /// </summary>
        /// <param name="ApplicationEntities">
        /// The lgevh entities.
        /// </param>
        public void Store(DbContext ApplicationEntities)
        {
            if (HttpContext.Current.Items.Contains(_dataContextKey))
            {
                HttpContext.Current.Items[_dataContextKey] = ApplicationEntities;
            }
            else
            {
                HttpContext.Current.Items.Add(_dataContextKey, ApplicationEntities);
            }
        }
    }
}
